package com.sjtu.vfact.visualization.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ListCellRenderer;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout2;
import edu.uci.ics.jung.algorithms.layout.KKLayout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout2;


public class GraphFrame extends JFrame {

	private static final long serialVersionUID = 6437353701220993518L;
	
	public static final String FR = "FRLayout";
	public static final String FR2 = "FRLayout2";
	public static final String CIRCLE = "CircleLayout";
	public static final String KK = "KKLayout";
	public static final String SPRING = "SpringLayout";
	public static final String SPRING2 = "SpringLayout2";
	
	private JPanel contentPane;
	private JPanel viewPanel;
//	private GraphPanel graphPanel;
	
	private JButton btnCluster;
	private JButton btnUncluster;
	private JRadioButton rdBtnOn;
	private JRadioButton rdBtnOff;

	private JComboBox cmbBoxLayout;

	private JComboBox cmbBoxClusterLayout;

	/**
	 * Create the frame.
	 */
	public GraphFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 951, 701);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		viewPanel = new JPanel();
		viewPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		contentPane.add(viewPanel, BorderLayout.CENTER);
		
//		graphPanel = new GraphPanel();
		
		JPanel panelRight = new JPanel();
		contentPane.add(panelRight, BorderLayout.EAST);
		panelRight.setLayout(new BorderLayout(0, 0));
		
		JPanel editorPanel = new JPanel();
		editorPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		editorPanel.setPreferredSize(new Dimension(300, 10));
		panelRight.add(editorPanel, BorderLayout.CENTER);
		editorPanel.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Clustering", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), new BevelBorder(BevelBorder.LOWERED, null, null, null, null)));
		panel.setBounds(6, 6, 288, 108);
		editorPanel.add(panel);
		panel.setLayout(null);
		
		btnCluster = new JButton("Cluster");
		btnCluster.setBounds(46, 29, 87, 29);
		panel.add(btnCluster);
		
		btnUncluster = new JButton("Uncluster");
		btnUncluster.setBounds(138, 29, 104, 29);
		panel.add(btnUncluster);
		
		JLabel lblClusterLayout = new JLabel("Layout : ");
		lblClusterLayout.setBounds(17, 70, 61, 16);
		panel.add(lblClusterLayout);
		
//		@SuppressWarnings("rawtypes")
//		Class[] cmbBoxClusterLayoutModel = new Class[]{FRLayout.class, FRLayout2.class, CircleLayout.class, 
//				KKLayout.class, SpringLayout.class, SpringLayout2.class};
		String[] cmbBoxClusterLayoutModel = new String[]{FR, FR2, CIRCLE, KK, SPRING, SPRING2};
		cmbBoxClusterLayout = new JComboBox(cmbBoxClusterLayoutModel);
		cmbBoxClusterLayout.setBounds(79, 66, 188, 27);
		cmbBoxClusterLayout.setSelectedItem(FR);
		panel.add(cmbBoxClusterLayout);
		
		JPanel panelAnimation = new JPanel();
		panelAnimation.setBorder(new TitledBorder(null, "Animation", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelAnimation.setBounds(6, 126, 188, 59);
		editorPanel.add(panelAnimation);
		
		rdBtnOn = new JRadioButton("On");
		panelAnimation.add(rdBtnOn);
		
		rdBtnOff = new JRadioButton("Off");
		panelAnimation.add(rdBtnOff);
		
		ButtonGroup animationGroup = new ButtonGroup();
		animationGroup.add(rdBtnOn);
		animationGroup.add(rdBtnOff);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Layout", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(6, 197, 276, 67);
		editorPanel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblLayout = new JLabel("Layout : ");
		lblLayout.setBounds(18, 31, 54, 16);
		panel_1.add(lblLayout);
		
//		@SuppressWarnings("rawtypes")
//		Class[] cmbBoxLayoutModel = new Class[]{FRLayout.class, FRLayout2.class, CircleLayout.class, 
//				KKLayout.class, SpringLayout.class, SpringLayout2.class};
		String[] cmbBoxLayoutModel = new String[]{FR, FR2, CIRCLE, KK, SPRING, SPRING2};
		cmbBoxLayout = new JComboBox(cmbBoxLayoutModel);
		cmbBoxLayout.setBounds(84, 27, 173, 27);
		cmbBoxLayout.setSelectedItem(FR);
		panel_1.add(cmbBoxLayout);
		
	}
	
	public void displayFrame(boolean state){
		setVisible(state);
	}
	
	public void generateGraph(GraphPanel graphPanel){
		viewPanel.add(graphPanel.getGZSP());
	}
	
	public void generateSSAGraph(GraphPanel graphPanel){
		viewPanel.add(graphPanel.getGZSP());
	}
	
	public Object getSelectedLayoutObject(){
		return cmbBoxLayout.getSelectedItem();
	}
	
	public Object getSelectedClusterLayoutObject(){
		return cmbBoxClusterLayout.getSelectedItem();
	}
	
	public void addBtnClusterListener(ActionListener l){
		btnCluster.addActionListener(l);
	}
	
	public void addBtnUnclusterListener(ActionListener l){
		btnUncluster.addActionListener(l);
	}
	
	public void addRdBtnAnimationOnListener(ActionListener l){
		rdBtnOn.addActionListener(l);
	}
	
	public void addRdBtnAnimationOffListener(ActionListener l){
		rdBtnOff.addActionListener(l);
	}
	
	public void setCmbBoxLayoutRenderer(ListCellRenderer renderer){
		cmbBoxLayout.setRenderer(renderer);
	}
	
	public void setCmbBoxClusterLayoutRenderer(ListCellRenderer renderer){
		cmbBoxClusterLayout.setRenderer(renderer);
	}
	
	public void addCmbBoxLayoutListener(ItemListener l){
		cmbBoxLayout.addItemListener(l);
	}
	
	public void addCmbBoxClusterLayoutListener(ItemListener l){
		cmbBoxClusterLayout.addItemListener(l);
	}
}
